/*==============================================================================
FILE NAME: Figures_B1_B2_Table_B1.do
PURPOSE: This .do file creates appendix figures and tables that are located in the appendix.
CREATED: 1 July 2025
==============================================================================*/


/* Set directory if working independently through code
if c(username)=="" { //insert username
	global rootdir "" // insert root path
	global processed_data "$rootdir/processed_data" 
	global figures "$rootdir/output/figures"  
	global tables "$rootdir/output/tables" 
	global point_estimates "$figures/point_estimates" // Define global paths for replication package
} 
*/

* Create binary variables for demographic and educational characteristics
set scheme modern
import delimited "$processed_data/citizen_complaints_clean.csv", clear
gen female = 0
replace female = 1 if sex == "Female"
replace female = . if sex == "Other" | sex == "Prefer not to say"

gen under_45 = 0
replace under_45 = 1 if age == "18-24" | age == "25-34" | age == "35-44"
replace under_45 = . if age == "Prefer not to say"

gen hs_grad = 0
replace hs_grad = 1 if education == "High school graduate, diploma, or the equivalent (for example: GED)"
gen some_college = 0
replace some_college = 1 if education == "Bachelor's degree" | education == "Master's degree" | education == "Other graduate or professional degree" | education == "Some college credit, no degree"
replace hs_grad = . if education == "Prefer not to say"
replace some_college =. if education == "Prefer not to say"

gen hisp = 0
replace hisp = 1 if ethnicity == "Hispanic"
replace hisp = . if ethnicity == "Prefer not to say"

gen black = 0 
replace black = 1 if race == "Black"
gen white = 0
replace white = 1 if race == "White"
gen other = 0
replace other = 1 
replace other = 0 if black == 1 | white == 1
replace other = . if race == "Prefer not to say"
replace black = . if race == "Prefer not to say"
replace white = . if race == "Prefer not to say"

replace black = 0 if hisp == 1
replace white = 0 if hisp == 1
replace other = 0 if hisp == 1
replace black = . if hisp == .
replace white = . if hisp == .
replace other = . if hisp == .
replace hisp = . if black == .

* Create political party affiliation variables
gen rep = 0 
replace rep = 1 if politics == "Republican"
gen dem = 0
replace dem = 1 if politics == "Democrat"
gen ind = 0
replace ind = 1 if politics == "Independent"
gen pref = 0 
replace pref = 1 if politics == "Prefer not to say"

* Generate outcome variable: ever reporting a pollution concern
gen yes = 0
replace yes = 1 if ever == "Yes"
replace yes = . if ever == ""

* Run regressions for the variable "yes" against demographics
foreach x in white black hisp other some_college under_45 female rep dem ind pref {
reg yes `x', r
est store `x'
}

* Label the demographic variables
label var white "Non-Hispanic White"
label var black "Non-Hispanic Black"
label var hisp "Hispanic"
label var other "Other Race/Ethnicity"
label var some_college "Some College Experience"
label var under_45 "< Age 45"
label var female "Female"
label var rep "Republican"
label var dem "Democrat"
label var ind "Independent"
label var pref "Prefer Not to Say"
label var yes "Reported a Pollution Concern"

* Create x rank for each var
foreach y in better_information ej basic_rights bad_info undue_influence {
cap drop x
gen x = 1 if `y' == "Strongly agree"
replace x = 2 if `y' == "Agree"
replace x = 3 if `y' == "Neither agree nor disagree"
replace x = 4 if `y'  == "Disagree"
replace x = 5 if `y' == "Strongly disagree"

* Create binary: agree = Strongly agree or agree
gen agree = 0
replace agree = 1 if x == 1 | x == 2
replace agree = . if x == .

* Run regressions for agreement vs demographics/politics
foreach x in yes white black hisp other some_college under_45 female rep dem ind pref yes {
reg agree `x', r
est store `x'
}

* Assign figure name (Figure B1)
local filename ""
if "`y'" == "better_information" local filename "Figure_B1_Panel_A"
else if "`y'" == "ej" local filename "Figure_B1_Panel_B"
else if "`y'" == "basic_rights" local filename "Figure_B1_Panel_C"
else if "`y'" == "bad_info" local filename "Figure_B1_Panel_D"
else if "`y'" == "undue_influence" local filename "Figure_B1_Panel_E"

* Create plot
graph set window fontface "Times New Roman"
coefplot yes white black hisp other some_college under_45 female rep dem ind pref, xline(0) pstyle(p1) ciopts(recast(rcap)) graphregion(fcolor(white)) drop(_cons) offset(0) mcolor(navy) msize(medsmall) mfcolor(navy) ylabel(,labsize(large)) xtitle("Strongly Agree or Agree", size(large)) legend(off) graphregion(fcolor(255 255 255)) graphregion(lcolor(255 255 255)) legend(off) xlabel(-0.25(0.05)0.25, nogrid labsize(large)) name(rankchange_bv, replace) xsize(8.6)

* Export figure
graph export "$figures/`filename'.pdf", replace

cap drop agree


* Export regression coefficients for Figure B1 as CSV
esttab yes white black hisp other some_college under_45 female rep dem ind pref using "$point_estimates/Point_estimates_`filename'.csv", replace se label csv


}

//Summary stats (Table B1)

* Convert duration to minutes
destring duration, replace
replace duration = duration/60

* Convert responses into numeric variables
foreach var in better_information ej better_job basic_rights bad_info undue_influence {
	gen `var'_num = .
	replace `var'_num = 1 if `var' == "Strongly agree"
	replace `var'_num = 2 if `var' == "Agree"
	replace `var'_num = 3 if `var' == "Neither agree nor disagree"
	replace `var'_num = 4 if `var' == "Disagree"
	replace `var'_num = 5 if `var' == "Strongly disagree"
}

* Numeric version of "ever" for "ever reported a complaint"
gen ever_num = .
replace ever_num = 1 if ever == "Yes"
replace ever_num = 0 if ever == "No"


* Flag respondents with complete race response
gen valid_race_response = !(hisp == 0 & black == 0 & white == 0 & other == 0)

* Panel A: main outcomes based on survey
estpost sum ever_num better_information_num ej_num basic_rights_num bad_info_num undue_influence_num
esttab using "$tables/Table_B1_Panel_A.tex", ///
	replace label tex ///
	cells("mean(fmt(2)) sd(fmt(2)) count(fmt(0))") ///
	collabels("Mean" "Std. Dev." "N")

* Panel B: demographics/politics
estpost sum female under_45 some_college hisp black white  other duration if valid_race_response == 1 
esttab using "$tables/Table_B1_Panel_B.tex",  ///
	replace label tex ///
	cells("mean(fmt(2)) sd(fmt(2)) count(fmt(0))") ///
	collabels("Mean" "Std. Dev." "N")
		
estpost sum rep dem ind if pref == 0 
esttab using "$tables/Table_B1_Panel_B_politics.tex",  ///
	replace label tex ///
	cells("mean(fmt(2)) sd(fmt(2)) count(fmt(0))") ///
	collabels("Mean" "Std. Dev." "N")
		
//Texas sample restriction

* Keep only Texas respondents (handles typos)
keep if state == "texas" | state == "texsa" | state == "Tx" | state == "Texss" | state == "Texas " | state == "Texas" | state == "Tecas" | state == "Taxes" | state == "TX" | state == "TEXAS" | state == "State of Texas" 

* Regressions for Texas
foreach x in white black hisp other some_college under_45 female rep dem ind pref {
reg yes `x', r
est store `x'
}

* Bar chart for Texas
foreach y in better_information better_job ej bad_info basic_rights undue_influence {
cap drop x
gen x = 1 if `y' == "Strongly agree"
replace x = 2 if `y' == "Agree"
replace x = 3 if `y' == "Neither agree nor disagree"
replace x = 4 if `y'  == "Disagree"
replace x = 5 if `y' == "Strongly disagree"


* Set custom file names
// File names reference a specific category, which is often cited in the paper
local filename ""
if "`y'" == "better_information" local filename "Figure_B2_Panel_A"
else if "`y'" == "ej" local filename "Figure_B2_Panel_B"
else if "`y'" == "basic_rights" local filename "Figure_B2_Panel_C"
else if "`y'" == "bad_info" local filename "Figure_B2_Panel_D" //bad_info known as wastes resources in manuscript
else if "`y'" == "undue_influence" local filename "Figure_B2_Panel_E"

* Plot horizontal bar chart
cap drop n
gen n = 1
graph set window fontface "Times New Roman"
graph hbar (sum) n, ///
    over(`y', sort(x) label(labsize(large))) ///
    ytitle("# of Responses", size(large)) ///
    ylab(, nogrid labsize(large))
* Graph export "`y'.png", replace
graph export "$figures/`filename'.pdf", replace


* Export frequencies for Figure B2 as CSV
// Frequencies used in Appendix B.4 as in-text numbers
preserve
collapse (count) count=n, by(`y')
egen total_responses = sum(count)
gen proportion = count / total_responses
gen percentage = proportion * 100
export delimited using "$point_estimates/Point_estimates_`filename'.csv", replace
restore

}



